using System;
using System.Collections.Generic;
using System.Net;
using Godot;

public partial class ActorHero : ActorBase
{
    private bool canFollow = false;
    public ActorHero(int actId) : base(actId)
    {

        actorType = ActorType.Hero;
        Position = new Vector2(400, 400);
        ZIndex = 10;
    }

    /// <summary>
    /// 升级操作
    /// </summary>
    public void upLevel()
    {
        //升级操作
        if (isCanLevelUp())
        {
            jobLevel++;
            var config = jobLevelData[jobLevel];

            Hp = (int)Math.Floor((1 + config.hpPlusRate) * Hp);
            Mp = (int)Math.Floor((1 + config.mpPlusRate) * Mp);
            power = (int)config.powerPlus + power;
            intelligence = (int)config.intelligencePlus + intelligence;
            agile = (int)config.agilePlus + agile;
            defense = (int)config.defensePlus + defense;
            moveRange = (int)config.moveRange;
        }
    }

    /// <summary>
    /// 更换职业
    /// </summary>
    /// <param name="newJobId">新的职业ID</param>
    public void ChangeJob(int newJobId)  //一般从最低级开始升级，但经验保持一致
    {
        if (newJobId == jobId)
        {
            return;
        }
        jobId = newJobId;
        var allJobLevelConfigs = ConfigManager.getInstance().findAllConfig<ConfigJobLevel>("ConfigJobLevel");
        jobLevelData.Clear();
        foreach (var config in allJobLevelConfigs)
        {
            if (config.jobId == newJobId)
            {
                if (config.level == 1)
                {
                    moveRange = config.moveRange;
                    Hp = (int)Math.Floor((1 + config.hpPlusRate) * initHp);
                    Mp = (int)Math.Floor((1 + config.mpPlusRate) * initMp);
                    power = (int)config.powerPlus + initPower;
                    intelligence = (int)config.intelligencePlus + initIntelligence;
                    agile = (int)config.agilePlus + initAgile;
                    defense = (int)config.defensePlus + initDefense;
                    moveRange = (int)config.moveRange;
                    jobLevel = 1;
                }
                jobLevelData.Add(config.level, config);


            }
        }

    }

    public bool isCanLevelUp()
    {
        if (jobLevelData.ContainsKey(jobLevel + 1))
        {
            var nextLevelExp = jobLevelData[jobLevel + 1].levelexp;
            if (exp > nextLevelExp)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            //满级了大佬
            return false;
        }

    }

    protected override void OnBtnPress()
    {
        base.OnBtnPress();
        /*if (actorState == ActorState.idle)
        {
            //暂停的时候才可以打开菜单
            OnShowActorMenu();
        }*/
    }

    public void EnableMouseFollow()
    {
        canFollow = true;
    }

    public void DisableMouseFollow()
    {
        canFollow = false;
    }

    public override void _Process(double delta)
    {
        base._Process(delta);
        if (canFollow)
        {
            this.Position = GetGlobalMousePosition();
        }
    }

    public ActorHero clone()
    {
        var result = new ActorHero(actId);
        result.exp = exp;
        result.Hp = Hp;
        result.Mp = Mp;
        result.power = power;
        result.intelligence = intelligence;
        result.agile = agile;
        result.defense = defense;
        result.jobId = jobId;
        result.leftMoveRange = leftMoveRange;
        result.leftMoveRange = jobLevel;
        result.actorType = actorType;
        result.isInScene = isInScene;
        result.actorState = actorState;
        result.leftActionTimes = leftActionTimes;
        result.Position = Position;
        return result;
    }
}